layui.use(['admin', 'treeTable', 'form', 'tool', 'xmSelect'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , tool = layui.tool
        , treeTable = layui.treeTable
        , request = setter.request
        , view = layui.view
        , form = layui.form;

    form.on('switch(menuStatusSwitch)', function(data){
        let id = $(data.elem).attr("data-id");
        let status = 0;
        if(data.elem.checked){
            status = 1;
        }
        let field = {"id": id, "status": status};
        admin.req({
            url: setter.request.domainName + '/admin/auth.menu/editStatus' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: false // 是否开启加载框
            , done: function (res) {

            }
        })
    });

    let tokenName = request.tokenName;
    let token = layui.data(setter.tableName)[request.tokenName];
    let headers = {};
    headers[tokenName] = token;
    treeTable.render({
        id: "menuTable"
        , elem: '#menuTable'
        , url: setter.request.domainName + '/admin/auth.menu/index?server=true'
        , headers: headers
        , tree: {
            iconIndex: 1,
            isPidData: true,
            customName: {
                id: 'id',//父ID
                pid: 'pid',//子ID
            },
        }
        , cols: [
            [
                {field: 'id', width: 90, title: 'ID'},
                {field: 'title',width: 260, title: "标题"},
                {field: 'name' ,width: 260, title: "名称"},
                {field: 'type', title: "类型", templet: function(d) {
                    let title = '';
                    if(d.type === 'menu_dir'){
                        title = "菜单目录";
                    }else if(d.type === 'menu'){
                        title = '菜单项';
                    }else if(d.type === 'button'){
                        title = '页面按钮';
                    }
                    return title;
                }},
                // {field: 'status', title: "状态", templet: "#menuStatusSwitch"},
                {field: 'create_time', title: "创建时间", templet: function(d) {
                        return tool.timestampToTime(d.create_time);
                    }
                },
                {fixed: 'right', width: 90, title: "操作", toolbar: '#menuTableTool'}
            ],
        ]
        , parseData: function(res){
            let code = res.code === 1 ? 0 : res.code;
            return {
                "code": code, // 解析接口状态
                "msg": res.msg, // 解析提示文本
                "count": res.data.total, // 解析数据长度
                "data": res.data.list // 解析数据列表
            };
        }
        , done: function () {
            //关闭加载
            layer.closeAll('loading');
        }
        , page: false
        , cellMinWidth: 80
    });

    treeTable.on('tool(menuTable)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            admin.popup({
                title: '编辑数据'
                , area: ['600px', '850px']
                , id: 'menu-popup-edit'
                , success: function (layero, index) {
                    let layId = this.id;
                    let field = {id: data.id};
                    // 获取菜单信息
                    admin.req({
                        url: setter.request.domainName + '/admin/auth.menu/edit'
                        , data: field
                        , type: 'get'
                        , loading: false // 是否开启加载框
                        , done: function (res) {
                            let initData = res.data.row;
                            initData['lay_index'] = index;
                            view(layId).render('auth/menu/edit', initData).done(function () {
                                form.render(null, 'layPopup');
                            });
                        }
                    })
                }
            });
        }
    });

})
